﻿using Models.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models.Device;
using Newtonsoft.Json;

using Models.TaskPlain;
using Microsoft.EntityFrameworkCore;
using GatewayServerService.Data;

using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.CloudCenter
{
    public class ServiceCloudDefaultAutoRun
    {
        //关于运行多了，会系统慢的问题：
        //传感器上传数据，普通不低于10秒执行一次
        //对于紧急条件处理，不低于5秒执行一次
        //对于任务处理，实时查询发送指令查询传感器




        public void defaultAutoExecuteCommandV2(string gwGuid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            try
            {

                List<iot_device_gateway> gateways = ef.iot_device_gateway.Where(c => c.gw_guid == gwGuid).ToList();//加条件
                foreach (iot_device_gateway itemgateway in gateways)
                {
                    //设备列表
                    List<iot_device_task_default_run> default_Runs =
                        (from d in ef.iot_device
                         join r in ef.iot_device_task_default_run on d.d_guid equals r.dr_d_guid
                         where d.d_gw_d_guid == itemgateway.gw_d_guid && r.dr_status == 0 && r.dr_complete == 0
                         select r).ToList();

                    //设备列表
                    foreach (iot_device_task_default_run item in default_Runs)
                    {



                        new ServiceCloudDeviceCommand().sendCommandToGateWay(item.dr_d_guid, item.dr_key, "自启动任务 dr_id:" + item.dr_id, "", (int)item.dr_repeat_count, new UtilsDateTime().getIntervalSeccond((double)item.dr_interval_value, item.dr_interval_unit));//执行

                        item.dr_execute_time = DateTime.Now;
                        item.dr_complete = 1;
                        ef.Update(item);
                        ef.SaveChanges();

                        ////设备运行参数
                        //List<iot_device_command> iot_Device_Commands = ef.iot_device_command.Where(c => c.dc_model_guid == itemDevice.d_model_guid && c.dc_status == 0).ToList();
                        //if (iot_Device_Commands.Count < 1)
                        //{
                        //    continue;
                        //}

                        //ModelCommandAgreement modelCommandAgreement = JsonConvert.DeserializeObject<ModelCommandAgreement>(itemDevice.d_parameter);
                        //ModelCommandAgreementConent modelCommandAgreementConent = UtilsObject.DynamicToEntity<ModelCommandAgreementConent>(modelCommandAgreement.content);
                        //if (modelCommandAgreementConent != null && modelCommandAgreementConent.executeCount != 0)
                        //{
                        //    //非无限次执行的话，退出，限次数的，还没实现
                        //    continue;
                        //}
                        //if (modelCommandAgreementConent == null)
                        //{
                        //    continue;
                        //}
                        //foreach (iot_device_command item in iot_Device_Commands)
                        //{
                        //判断间隔时间
                        //  if (!new ServiceCloudDeviceCommand().isOutSideExecuteTime(modelCommandAgreementConent.intervalTime, itemDevice.d_guid, item.dc_key))
                        //  {
                        //未到停时执行时间
                        //     continue;
                        // }
                        //new ServiceCloudDeviceCommand().setDeviceExecuteTime(itemDevice.d_guid, item.dc_key);//更新执行时间

                        //  new ServiceCloudDeviceCommand().sendCommandToGateWay(itemDevice.d_guid, item.dc_key, "", modelCommandAgreementConent.executeCount, modelCommandAgreementConent.intervalTime, modelCommandAgreementConent.intervalUnit);//执行
                        // }
                    }
                }
                ef.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }


        }

        public void resetDeviceTaskDefaultRun(string gwGuid)
        {
            try
            {
                EFYihongIotContext ef = new EFYihongIotContext();
                List<iot_device_gateway> gateways = ef.iot_device_gateway.Where(c => c.gw_guid == gwGuid).ToList();//加条件
                foreach (iot_device_gateway itemgateway in gateways)
                {
                    //设备列表
                    List<iot_device_task_default_run> default_Runs =
                        (from d in ef.iot_device
                         join r in ef.iot_device_task_default_run on d.d_guid equals r.dr_d_guid
                         where d.d_gw_d_guid == itemgateway.gw_d_guid && r.dr_status == 0
                         select r).ToList();

                    //设备列表
                    foreach (iot_device_task_default_run item in default_Runs)
                    {
                        item.dr_complete = 0;
                        item.dr_mod_time = DateTime.Now;
                        ef.Update(item);
                        ef.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex); }

            //执行 运行指令
            defaultAutoExecuteCommandV2(gwGuid);
        }


    }

}
